package com.admin.service;

import com.admin.dao.FeeDao;
import com.admin.entity.Fee;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//费用信息接口类
@Service
public class FeeService extends BaseService {

    @Autowired
    private FeeDao feeDao;

    //【费用信息】设置查询条件
    private LambdaQueryWrapper<Fee> getFeeQueryCondition(Fee fee) {
        LambdaQueryWrapper<Fee> lambdaQuery = this.getBaseQueryCondition(fee);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Fee::getCreateDate);
        if (ObjectUtils.isNotNull(fee)) {
            //【费用编号】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(fee.getFeeCode()), Fee::getFeeCode, fee.getFeeCode());
            //【用户姓名】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(fee.getRegisterId()), Fee::getRegisterId, fee.getRegisterId());
            //【费用类型】范围查询
            lambdaQuery.in(ObjectUtils.isNotNull(fee.getFeeTypeRange()), Fee::getFeeType, fee.getFeeTypeRange());
            //【费用类型】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(fee.getFeeType()), Fee::getFeeType, fee.getFeeType());
            //【费用金额】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(fee.getAmount()), Fee::getAmount, fee.getAmount());
            //【费用日期】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(fee.getFeeDate()), Fee::getFeeDate, fee.getFeeDate());
            //【费用日期】范围查询
            if (ObjectUtils.isNotNull(fee.getFeeDateRange()) && fee.getFeeDateRange().size() > 0) {
                lambdaQuery.between(Fee::getFeeDate, fee.getFeeDateRange().get(0), fee.getFeeDateRange().get(1));
            }
            //【费用状态】范围查询
            lambdaQuery.in(ObjectUtils.isNotNull(fee.getFeeStatusRange()), Fee::getFeeStatus, fee.getFeeStatusRange());
            //【费用状态】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(fee.getFeeStatus()), Fee::getFeeStatus, fee.getFeeStatus());
        }
        return lambdaQuery;
    }

    //【费用信息】分页查询
    public IPage<Fee> getPage(Page<Fee> page, Fee fee) {
        LambdaQueryWrapper<Fee> lambdaQuery = getFeeQueryCondition(fee);
        return feeDao.selectPage(page, lambdaQuery);
    }
    
    //【费用信息】查询列表
    public List<Fee> getList(Fee fee) {
        LambdaQueryWrapper<Fee> lambdaQuery = getFeeQueryCondition(fee);
        return feeDao.selectList(lambdaQuery);
    }
    
    //【费用信息】根据id查询
    public Fee get(String id) {
        return feeDao.selectById(id);
    }

    //【费用信息】根据对象查询
    public Fee get(Fee fee) {
    LambdaQueryWrapper<Fee> lambdaQuery = getFeeQueryCondition(fee);
        return feeDao.selectOne(lambdaQuery);
    }
    
    //【费用信息】新增
    public int insert(Fee fee) {
        this.preInsert(fee);
        return feeDao.insert(fee);
    }
    
    //【费用信息】修改
    public int update(Fee fee) {
        this.preUpdate(fee);
        return feeDao.updateById(fee);
    }
    
    //【费用信息】删除
    public int delete(String id) {
        return feeDao.deleteById(id);
    }

    //【费用信息】批量删除
    public int delAll(String[] ids) {
        return feeDao.deleteBatchIds(Arrays.asList(ids));
    }

}